package com.zyyoona7.wheelpicker.slice;

import com.zyyoona7.picker.CityPicker;
import com.zyyoona7.picker.CityPickerDialogFragment;
import com.zyyoona7.picker.listener.DialogPickerValueChange;
import com.zyyoona7.picker.util.SelectButton;
import com.zyyoona7.wheelpicker.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Picker;
import ohos.agp.components.TextField;
import ohos.agp.components.Checkbox;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * picker
 *
 * @since 2021-04-09
 */
public class MainAbility4Slice extends AbilitySlice implements Component.ClickedListener {
    private static final int num6 = 6;
    private static final int num2 = 2;
    private static final int num8 = 8;
    private static final int num20 = 20;
    private static final int num12 = 12;

    /**
     * 城市选择框
     */
    private SelectButton citySlect;
    private Picker pickerOne;
    private Picker pickerTwo;
    private Picker pickerThree;
    private Picker pickerFour;
    private Picker pickerFive;
    private TextField editOne;
    private TextField editTwo;
    private TextField editThree;
    private SelectButton signone;
    private SelectButton signtwo;
    private SelectButton signthree;
    private CityPicker pickerCity;
    private Checkbox checkbox;

    /**
     * onStart
     *
     * @param intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main4);
        initView();
        initDate();
        initComponent();
    }

    private void initView() {
        findId();
        setData1();
        List<String> ll2 = initpickerdata2();
        pickerTwo.setMinValue(0);
        pickerTwo.setMaxValue(ll2.size() - 1);
        Picker.Formatter provinceFormatter2 = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return ll2.get(i);
            }
        };
        pickerTwo.setFormatter(provinceFormatter2);
        List<String> ll3 = initpickerdata2();
        pickerThree.setMinValue(0);
        pickerThree.setMaxValue(ll3.size() - 1);
        Picker.Formatter provinceFormatter3 = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return ll3.get(i);
            }
        };
        pickerThree.setFormatter(provinceFormatter3);
        List<String> ll4 = initpickerdata2();
        pickerFour.setMinValue(0);
        pickerFour.setMaxValue(ll4.size() - 1);
        Picker.Formatter provinceFormatter4 = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return ll4.get(i);
            }
        };
        pickerFour.setFormatter(provinceFormatter4);
        List<String> ll5 = initpickerdata2();
        pickerFive.setMinValue(0);
        pickerFive.setMaxValue(ll5.size() - 1);
        Picker.Formatter provinceFormatter5 = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return ll5.get(i);
            }
        };
        pickerFive.setFormatter(provinceFormatter5);
    }

    private void setData1() {
        List<String> ll = initpickerdata();
        pickerOne.setMinValue(0);
        pickerOne.setMaxValue(ll.size() - 1);
        Picker.Formatter provinceFormatter = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return ll.get(i);
            }
        };
        pickerOne.setFormatter(provinceFormatter);
    }

    private void findId() {
        citySlect = (SelectButton) findComponentById(ResourceTable.Id_city_slect);
        checkbox = (Checkbox) findComponentById(ResourceTable.Id_checkbox);
        pickerOne = (Picker) findComponentById(ResourceTable.Id_picker_one);
        pickerTwo = (Picker) findComponentById(ResourceTable.Id_picker_two);
        pickerThree = (Picker) findComponentById(ResourceTable.Id_picker_three);
        pickerFour = (Picker) findComponentById(ResourceTable.Id_picker_four);
        pickerFive = (Picker) findComponentById(ResourceTable.Id_picker_five);
        pickerCity = (CityPicker) findComponentById(ResourceTable.Id_picker_city);
        editOne = (TextField) findComponentById(ResourceTable.Id_edit_one);
        editTwo = (TextField) findComponentById(ResourceTable.Id_edit_two);
        editThree = (TextField) findComponentById(ResourceTable.Id_edit_three);
        signone = (SelectButton) findComponentById(ResourceTable.Id_signone);
        signtwo = (SelectButton) findComponentById(ResourceTable.Id_signtwo);
        signthree = (SelectButton) findComponentById(ResourceTable.Id_signthree);
        signone.setClickedListener(MainAbility4Slice.this);
        signtwo.setClickedListener(MainAbility4Slice.this);
        signthree.setClickedListener(MainAbility4Slice.this);
        citySlect.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showDialog();
            }
        });

        checkbox.addDrawTask((component, canvas) -> drawCheckBox(component, canvas));
        checkbox.setTouchEventListener((component, touchEvent) -> {
            component.invalidate();
            return true;
        });
    }

    private void drawCheckBox(Component component, Canvas canvas) {
        final double height = component.getHeight();
        Paint paint = new Paint();
        paint.setColor(new Color(Color.getIntColor("#FFE337BC")));
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setStrokeWidth(num6);
        canvas.drawCircle((float)(height / num2 - num8), (float)height / num2, num20, paint);
        if (((Checkbox) component).isChecked()) {
            Paint paint2 = new Paint();
            paint2.setColor(new Color(Color.getIntColor("#FFE337BC")));
            paint2.setStyle(Paint.Style.FILL_STYLE);
            canvas.drawCircle((float)(height / num2 - num8), (float) (height / num2), num12, paint2);
        }
    }

    private void showDialog() {
        CityPickerDialogFragment cityPickerDialogFragment = new CityPickerDialogFragment(getAbility());
        cityPickerDialogFragment.setDialogPickerValueChange(new DialogPickerValueChange() {
            @Override
            public void valueChange(String message) {
                new ToastDialog(getContext())
                        .setText(message)
                        // Toast显示在界面中间
                        .setAlignment(LayoutAlignment.CENTER)
                        .show();
            }
        });
        cityPickerDialogFragment.show();
    }

    private List<String> initpickerdata() {
        List<String> list = new ArrayList<String>();
        list.add("zyyoona_0");
        list.add("zyyoona_1");
        list.add("zyyoona_2");
        list.add("zyyoona_3");
        list.add("zyyoona_4");
        list.add("zyyoona_5");
        list.add("zyyoona_6");
        list.add("zyyoona_7");
        list.add("zyyoona_8");
        list.add("zyyoona_9");
        return list;
    }

    private List<String> initpickerdata2() {
        List<String> list = new ArrayList<String>();
        list.add("WheelPicker_0");
        list.add("WheelPicker_1");
        list.add("WheelPicker_2");
        list.add("WheelPicker_3");
        list.add("WheelPicker_4");
        list.add("WheelPicker_5");
        list.add("WheelPicker_6");
        list.add("WheelPicker_7");
        list.add("WheelPicker_8");
        list.add("WheelPicker_9");
        return list;
    }

    private void initDate() {
    }

    private void initComponent() {
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     *
     * @param intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * onClick
     *
     * @param component
     */
    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_signone:
                String text = editOne.getText();
                if (!"".equals(text)) {
                    if (isNum(text)) {
                        int sign = Integer.parseInt(text);
                        pickerCity.setOnePickerValue(sign);
                    }
                }

                break;
            case ResourceTable.Id_signtwo:
                String text2 = editTwo.getText();
                if (!"".equals(text2)) {
                    if (isNum(text2)) {
                        int sign2 = Integer.parseInt(text2);
                        pickerCity.setTwoPickerValue(sign2);
                    }
                }

                break;
            case ResourceTable.Id_signthree:
                String text3 = editThree.getText();
                if (!"".equals(text3)) {
                    if (isNum(text3)) {
                        int sign3 = Integer.parseInt(text3);
                        pickerCity.setThreePickerValue(sign3);
                    }
                }

                break;
            default:
                break;
        }
    }

    /**
     * 是否为数字
     *
     * @param num 字符串
     * @return 是否
     */
    public boolean isNum(String num) {
        boolean isNums = false;
        Pattern pattern = Pattern.compile("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
        Matcher isNum = pattern.matcher(num);
        if (isNum.matches()) {
            isNums = true;
        }
        return isNums;
    }
}
